package view.main;

import info.clearthought.layout.TableLayout;
import java.awt.Container;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import model.game.Difficulty;
import model.game.GagalPanenEngine;
import model.game.GameController;
import model.game.GameEngine;
import model.game.InformationLayer;

public class Main extends JFrame {

  private static final long serialVersionUID = 2L;

  private void startGame(final GameEngine engine) {
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
      public void run() {
	UserInterface UI = new UserInterface(engine);
        //MainView app = new MainView(new GameController(engine),
        //        new InformationLayer(engine));

      }
    });
  }

  private GagalPanenEngine mainengine_;
  public Main() {

    this.setTitle("Gagal Panen 2");

    mainengine_ = new GagalPanenEngine();

    Container cp = this.getContentPane();

    // sets the layout using table layout
    double[][] table_size = {
        {400.0},
        {200.0,64.0,64.0}
    };

    // table layout if x, y

    cp.setLayout(new TableLayout(table_size));

    cp.add(new JLabel("Gagal Panen 2"), "0, 0");

    JButton newgame = new JButton();
    newgame.setText("New Game");
    newgame.addMouseListener(
      new MouseAdapter() {
        @Override
        public void mouseReleased(MouseEvent e) {
          String username = JOptionPane.showInputDialog("Hero Name");
          if (username == null) return;

          Object[] options = { "Easy", "Medium", "Hard", "..." };
          int choice = JOptionPane.showOptionDialog(null, "Select difficulty", "Difficulty",
          JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE,
          null, options, options[0]);
          Difficulty diff = Difficulty.kEasy;
          if (choice == JOptionPane.CLOSED_OPTION) return;
          switch (choice) {
            case 0:
              diff = Difficulty.kEasy;
              break;
            case 1:
              diff = Difficulty.kMedium;
              break;
            case 2:
              diff = Difficulty.kHard;
              break;
            case 3:
              diff = Difficulty.kImpossible;
              break;
            default:
              assert false;
              diff = Difficulty.kEasy;
          }
          startGame(mainengine_.newGame(username, diff));
        }
      }
    );
    cp.add(newgame, "0, 1");

    JButton loadgame = new JButton();
    loadgame.setText("Load Game");
    loadgame.addMouseListener(
      new MouseAdapter() {
        @Override
        public void mouseReleased(MouseEvent e) {
          String username = JOptionPane.showInputDialog("Hero Name");
          if (username == null) return;
          GameEngine engine = mainengine_.loadGame(username + ".gag");
          if (engine != null) {
            startGame(engine);
          } else {
            JOptionPane.showMessageDialog(Main.this, "File not found", "Error",
                    JOptionPane.ERROR_MESSAGE);
          }
        }
      }
    );
    cp.add(loadgame, "0, 2");

    this.pack();

    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    this.setVisible(true);
  }

  public static void main(String [] args) {
		javax.swing.SwingUtilities.invokeLater(new Runnable() {
          public void run() {
            Main app = new Main();
          }
		});
	}

}
